<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/8/14
 * Time: 15:05
 */

namespace App\Http\Controllers\Web;


use App\Service\FloorRoomService;
use EasyWeChat\Kernel\Http\Response;

class FloorRoomController extends BaseController
{
    public function index()
    {
        $params = $this->request->post();
        $page = $this->request->post('page',1);
        $limit = $this->request->post('limit',50);
        return FloorRoomService::getList($params,'order','ASC',$page,$limit);

    }

    public function getFloorRoomById()
    {
        $roomId = $this->request->post('id');
        if(!$roomId){
            return Response()->json(['code'=>201,'msg'=>'参数缺失']);
        }
        return FloorRoomService::getFloorRoomById($roomId);
    }

    public function add()
    {
        $params = $this->request->post();
        if(!isset($params['name']) || empty(trim($params['name']))){
            return Response()->json(['code'=>201,'msg'=>'楼层名称不能为空']);
        }
        if(!isset($params['building_no']) || empty(intval($params['building_no']))){
            return Response()->json(['code'=>201,'msg'=>'栋数号不能为空']);
        }
        if(!isset($params['floor_no']) || empty(intval($params['floor_no']))){
            return Response()->json(['code'=>201,'msg'=>'楼层数号不能为空']);
        }
        if(!isset($params['room_no']) || empty(trim($params['room_no']))){
            return Response()->json(['code'=>201,'msg'=>'房间号不能为空']);
        }
        return FloorRoomService::add($params);
    }
    
    public function addBatch()
    {
        $params = $this->request->post();
        return FloorRoomService::addBatch($params);
    }

    public function edit()
    {
        $params = $this->request->post();
        if(!isset($params['id']) || empty(intval($params['id']))){
            return Response()->json(['code'=>201,'msg'=>'参数缺失']);
        }
        if(!isset($params['name']) || empty(trim($params['name']))){
            return Response()->json(['code'=>201,'msg'=>'楼层名称不能为空']);
        }
        if(!isset($params['building_no']) || empty(intval($params['building_no']))){
            return Response()->json(['code'=>201,'msg'=>'栋数号不能为空']);
        }
        if(!isset($params['floor_no']) || empty(intval($params['floor_no']))){
            return Response()->json(['code'=>201,'msg'=>'楼层数号不能为空']);
        }
        if(!isset($params['room_no']) || empty(trim($params['room_no']))){
            return Response()->json(['code'=>201,'msg'=>'房间号不能为空']);
        }
        return FloorRoomService::edit($params);
    }

    public function destroy()
    {
        $ids = $this->request->post('ids');
        $ids = is_array($ids) ? $ids : [$ids];
        if(!$ids){
            return Response()->json(['code'=>201,'msg'=>'请选择要删除的楼房']);
        }
        return FloorRoomService::destroy($ids);
    }

}
